package com.ctntest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;

import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;

@SuppressWarnings("unchecked")
public class ApkTest extends ActivityInstrumentationTestCase2{

	public static final int CLICK_SCREEN_LONG            = 3;
	public static final int CLICK_ACTION_BAR_HOME_BUTTON = 7;
	public static final int CLICK_ACTION_BAR_ITEM        = 8;
	public static final int CLICK_MENU_ITEM              = 14;
	public static final int CLICK_SCREEN                 = 16;
	public static final int DRAG                         = 20;
	public static final int GO_BACK                      = 21;
	public static final int PRESS_MENU_ITEM              = 22;
	public static final int SCROLL_DOWN                  = 24;
	public static final int SCROLL_BOTTOM                = 25;
	public static final int SCROLL_TOP                   = 26;
	public static final int SCROLL_SIDE                  = 27;
	public static final int SCROLL_UP                    = 28;
	public static final int SET_ACTIVITY_ORIENTATION     = 30;

	private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.CountToNine.mobile.Load";
	private static Class launcherActivityClass;

	static {
		try{
			launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
		} catch (ClassNotFoundException e){
			throw new RuntimeException(e);
		}
	}

	public ApkTest()throws ClassNotFoundException {
		super(launcherActivityClass);
	}

	private Solo solo;

	@Override
	protected void setUp() throws Exception {
		solo = new Solo(getInstrumentation(),getActivity());
	}

	/**
	 *
	 */
	public void testRandomBlackBox() {
		BufferedReader in = null;
		String path = "data/local/tmp/teste.txt";
		Random generator = new Random();

		try {
			String sCurrentLine;
			in = new BufferedReader(new FileReader(path));

			while ((sCurrentLine = in.readLine()) != null) {
				int event = Integer.parseInt(sCurrentLine);

				switch (event) {
					case ApkTest.CLICK_SCREEN_LONG:
						solo.clickLongOnScreen(generator.nextInt(428) + 9, generator.nextInt(719) + 9);
						break;
					case ApkTest.CLICK_ACTION_BAR_HOME_BUTTON:
						solo.clickOnActionBarHomeButton();
						break;
					case ApkTest.CLICK_ACTION_BAR_ITEM:
						solo.clickOnActionBarItem(0);
						break;
					case ApkTest.CLICK_MENU_ITEM:
						solo.clickOnMenuItem("Start");
						break;
					case ApkTest.CLICK_SCREEN:
						solo.clickOnScreen(generator.nextInt(438), generator.nextInt(729));
						break;
					case ApkTest.DRAG:
						solo.drag(generator.nextInt(428) + 9, generator.nextInt(428) + 9,
								generator.nextInt(719) + 9, generator.nextInt(719) + 8, 5);
						break;
					case ApkTest.GO_BACK:
						solo.goBack();
						break;
					case ApkTest.PRESS_MENU_ITEM:
						solo.pressMenuItem(0);
						break;
					case ApkTest.SCROLL_DOWN:
						solo.scrollDown();
						break;
					case ApkTest.SCROLL_BOTTOM:
						solo.scrollToBottom();
						break;
					case ApkTest.SCROLL_TOP:
						solo.scrollToTop();
						break;
					case ApkTest.SCROLL_SIDE:
						solo.scrollToSide(SCROLL_SIDE);
						break;
					case ApkTest.SCROLL_UP:
						solo.scrollUp();
						break;
					case ApkTest.SET_ACTIVITY_ORIENTATION:
						solo.setActivityOrientation(SET_ACTIVITY_ORIENTATION);
						break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	@Override
	public void tearDown() throws Exception {
		solo.finishOpenedActivities();
	}
}